﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;

namespace KingSite.Library.Sms.EntinfoWrapper {
    public class EntinfoWrapper : ISms {

        public string SN {
            get { return ConfigurationManager.AppSettings["SMS_EntinfoSN"]; }
        }

        public string Password {
            get { return ConfigurationManager.AppSettings["SMS_EntinfoPassword"]; }
        }

        public bool Send(string mobileNumber, string message) {
            bool b = false;
            string password = Password;//密码  
            string sn = SN;//序列号
            string subcode = "";//扩展码
            string stime = "";//定时时间
            csmd_sms_ws.WebServiceSoapClient sms = new csmd_sms_ws.WebServiceSoapClient();
            //MD5加密密码
            string pwd = getMD5(sn + password);

            long startTime, endTime;
            startTime = DateTime.Now.Ticks;//记录当前时刻

            try {
                //发送短信
                //定时时间，扩展码和rrid可以为空，当rrid(唯一标识串)为空时，返回系统指定rrid
                string result = sms.mt(sn, pwd, mobileNumber, message, subcode, stime, "");

                endTime = DateTime.Now.Ticks;
                TimeSpan sendSpan = new TimeSpan(endTime - startTime);

                if (result.StartsWith("-")) {
                    if (result != "") {
                        b = false;
                    }
                }
                else {
                    if (result != "") {
                        b = true;
                    }
                }
            }
            catch {
                //发送失败
            }
            return b;                 
        }

        /// <summary>
        /// 获取md5码
        /// </summary>
        /// <param name="source">待转换字符串</param>
        /// <returns>md5加密后的字符串</returns>
        public string getMD5(string source) {
            string result = "";
            try {
                MD5 getmd5 = new MD5CryptoServiceProvider();
                byte[] targetStr = getmd5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(source));
                result = BitConverter.ToString(targetStr).Replace("-", "");
                return result;
            }
            catch (Exception) {
                return "0";
            }

        }

        /// <summary>
        /// Sends the async.
        /// </summary>
        /// <param name="mobileNumber">The mobile number.</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        /// 孟凡威 2011/11/2 9:44
        public bool SendAsync(string mobileNumber, string message)
        {
            string password = Password;//密码  
            string sn = SN;//序列号
            string subcode = "";//扩展码
            string stime = "";//定时时间
            async_csmd_sms_ws.WebServiceSoapClient sms = new async_csmd_sms_ws.WebServiceSoapClient();
            //MD5加密密码
            string pwd = getMD5(sn + password);

            try
            {
                //发送短信
                //定时时间，扩展码和rrid可以为空，当rrid(唯一标识串)为空时，返回系统指定rrid
                string result = string.Empty;
                sms.mtCompleted += sms_mtCompleted;
                sms.mtAsync(sn, pwd, mobileNumber, message, subcode, stime, "");

                return true;
            }
            catch
            {
                //发送失败
                return false;
            }
        }

        void sms_mtCompleted(object sender, async_csmd_sms_ws.mtCompletedEventArgs e)
        {
            string result = e.Result;
            long startTime, endTime;
            startTime = DateTime.Now.Ticks;//记录当前时刻
            endTime = DateTime.Now.Ticks;
            TimeSpan sendSpan = new TimeSpan(endTime - startTime);

        }
    }
}
